package com.example.hetong_system.repository;

import com.example.hetong_system.model.Contract;
import com.example.hetong_system.model.ContractClause;
import com.example.hetong_system.model.ContractClauseId;
import com.example.hetong_system.model.enums.AuditStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface ContractClauseRepository extends JpaRepository<ContractClause, ContractClauseId> {

    // 检查条款是否已关联到合同
    @Query("SELECT COUNT(cc) > 0 FROM ContractClause cc " +
            "WHERE cc.contract.id = :contractId AND cc.clause.id = :clauseId")
    boolean existsByContractIdAndClauseId(@Param("contractId") Long contractId, @Param("clauseId") Long clauseId);

    // 删除合同与条款的关联
    @Modifying
    @Query("DELETE FROM ContractClause cc WHERE cc.contract.id = :contractId AND cc.clause.id = :clauseId")
    void deleteByContractIdAndClauseId(@Param("contractId") Long contractId, @Param("clauseId") Long clauseId);

    /**
     * 统计待审核的合同数量（最终优化版）
     */
    @Query("SELECT COUNT(DISTINCT cc.contract) FROM ContractClause cc " +
            "WHERE (cc.legalAuditStatus = :status AND :isLegalAudit = true) OR " +
            "(cc.financeAuditStatus = :status AND :isLegalAudit = false)")
    Long countByAuditStatus(@Param("isLegalAudit") boolean isLegalAudit,
                            @Param("status") AuditStatus status);

    /**
     * 查询有待审核条款的合同（带分页，最终优化版）
     */
    @Query("SELECT DISTINCT cc.contract FROM ContractClause cc " +
            "WHERE (cc.legalAuditStatus = :status AND :isLegalAudit = true) OR " +
            "(cc.financeAuditStatus = :status AND :isLegalAudit = false)")
    Page<Contract> findContractsWithPendingAudit(
            @Param("isLegalAudit") boolean isLegalAudit,
            @Param("status") AuditStatus status,
            Pageable pageable);

    /**
     * 根据合同ID查询条款
     */
    @Query("SELECT cc FROM ContractClause cc WHERE cc.contract.id = :contractId")
    List<ContractClause> findByContractId(@Param("contractId") Long contractId);

    // 根据合同ID和审核状态查询条款（带分页）
    Page<ContractClause> findByContractIdAndLegalAuditStatus(
            @Param("contractId") Long contractId,
            @Param("status") AuditStatus status,
            Pageable pageable);

    Page<ContractClause> findByContractIdAndFinanceAuditStatus(
            @Param("contractId") Long contractId,
            @Param("status") AuditStatus status,
            Pageable pageable);



    /**
     * 统计待客户审核的合同数量
     */
    @Query("SELECT COUNT(DISTINCT cc.contract) FROM ContractClause cc " +
            "WHERE cc.customerAuditStatus = :status AND cc.contract.customer.id = :customerId")
    Long countByCustomerAuditStatusAndCustomer(
            @Param("status") AuditStatus status,
            @Param("customerId") Long customerId);

    /**
     * 查询有待客户审核条款的合同（带分页）
     */
    @Query("SELECT DISTINCT cc.contract FROM ContractClause cc " +
            "WHERE cc.customerAuditStatus = :status AND cc.contract.customer.id = :customerId")
    Page<Contract> findContractsWithPendingCustomerAudit(
            @Param("customerId") Long customerId,
            @Param("status") AuditStatus status,
            Pageable pageable);

    /**
     * 根据合同ID和客户审核状态查询条款（带分页）
     */
    Page<ContractClause> findByContractIdAndCustomerAuditStatus(
            @Param("contractId") Long contractId,
            @Param("status") AuditStatus status,
            Pageable pageable);
}